<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-left">
        <div class="login-brand">
          <h1>在线考试系统</h1>
          <p>专业、高效、智能的考试管理平台</p>
        </div>
      </div>
      <div class="login-right">
        <div class="login-form-container">
          <h2 class="login-title">欢迎登录</h2>
          <p class="login-subtitle">请使用您的账号登录系统</p>
          <el-form :model="loginForm" :rules="loginRules" ref="loginForm" class="login-form">
            <el-form-item prop="username">
              <el-input 
                v-model="loginForm.username" 
                prefix-icon="el-icon-user" 
                placeholder="请输入用户名"
                class="custom-input">
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input 
                v-model="loginForm.password" 
                prefix-icon="el-icon-lock" 
                placeholder="请输入密码" 
                show-password
                class="custom-input">
              </el-input>
            </el-form-item>
            <el-form-item prop="role" class="role-selector">
              <p class="role-label">选择您的角色</p>
              <el-radio-group v-model="loginForm.role">
                <el-radio :label="0">学生</el-radio>
                <el-radio :label="1">教师</el-radio>
                <el-radio :label="2">管理员</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleLogin" class="login-button" :loading="loading">登录系统</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginPage',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        role: 0
      },
      loading: false,
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 处理登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // 调用登录API
          this.$store.dispatch('login', this.loginForm)
            .then(() => {
              this.loading = false
              // 根据角色跳转到不同页面
              const role = this.$store.getters.getUserRole
              if (role === 'student') {
                this.$router.push('/student/exam')
              } else if (role === 'teacher' || role === 'admin') {
                this.$router.push('/admin/home')
              }
              this.$message.success('登录成功')
            })
            .catch(() => {
              this.loading = false
              // 错误信息已在拦截器中处理
            })
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100%;
  width: 100%;
  background: linear-gradient(135deg, var(--primary-dark), var(--primary-color));
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.login-content {
  display: flex;
  width: 900px;
  height: 550px;
  background-color: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
}

.login-left {
  flex: 1;
  background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  padding: 40px;
}

.login-brand {
  text-align: center;
}

.login-brand h1 {
  font-size: 36px;
  font-weight: 600;
  margin-bottom: 20px;
  letter-spacing: 1px;
}

.login-brand p {
  font-size: 16px;
  opacity: 0.9;
  line-height: 1.6;
}

.login-right {
  flex: 1;
  padding: 40px;
  display: flex;
  align-items: center;
}

.login-form-container {
  width: 100%;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: var(--primary-dark);
  margin-bottom: 10px;
}

.login-subtitle {
  color: var(--text-secondary);
  margin-bottom: 30px;
  font-size: 14px;
}

.login-form {
  margin-top: 20px;
}

.custom-input {
  height: 50px;
}

.custom-input >>> .el-input__inner {
  height: 50px;
  line-height: 50px;
  border: 1px solid var(--border-color);
  transition: all 0.3s;
}

.custom-input >>> .el-input__inner:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(30, 58, 138, 0.1);
}

.role-selector {
  margin-top: 20px;
}

.role-label {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.login-button {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  margin-top: 20px;
  background-color: var(--primary-color);
  border-color: var(--primary-color);
  transition: all 0.3s;
}

.login-button:hover {
  background-color: var(--primary-light);
  border-color: var(--primary-light);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(30, 58, 138, 0.2);
}
</style>